package com.woniuxy.service.impl;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.woniuxy.dao.FocusDao;
import com.woniuxy.entity.Focus;
import com.woniuxy.entity.PageBean;
import com.woniuxy.entity.User;
import com.woniuxy.service.FocusService;
import com.woniuxy.util.CommonUtil;
/**
 * 
 * @author 曾令忠
 *
 */
@Service
public class FocusServiceImpl implements FocusService {
	@Autowired
	private FocusDao focusDao;

	/**
	 * 查找关注我的用户
	 * fid:当前用户id
	 */
	@Override
	public List<Focus> findFocus(long fid) {
		List<Focus> focuses = focusDao.findFocus(fid);
		focuses = CommonUtil.focusSetInfo(fid, focuses);
		return focuses;
	}

	/**
	 * id关注人id,id2被关注人id
	 */
	@Override
	@Transactional
	public void cancel(long id, int id2) {
		// 先把这条关注的信息查出来，后面需要根据是不是好友决定 更改另一个的状态
		Focus focus = focusDao.getCertainFocus(id, (long) id2);
		// 删除关注记录
		focusDao.cancel(focus.getId());
		// 判断是不是好友
		if (focus.getFocusStatus().equals("2")) {
			// 更改好友的状态
			focusDao.updateFocusStatus((long) id2, id,"1");
		}

	}

	/**
	 * 添加关注
	 * id：关注人id；id2：被关注人id
	 */
	@Override
	@Transactional
	public void addFocus(long id, int id2) {
		// 先查询有没有关注过
		Focus focus1 = focusDao.getCertainFocus(id, (long) id2);
		if(focus1!=null) {
			return;
		}
		//再看有没有被关注过
		String status = "1";
		Focus focus2 = focusDao.getCertainFocus((long) id2,id);
		if(focus2!=null) {
			status = "2";
			focusDao.updateFocusStatus((long) id2, id,"2");
		}
		//添加关注
		Focus focus = new Focus();
		focus.setFocusFromId(id);
		focus.setFocusToId(id2);
		focus.setFocusStatus(status);
		focusDao.addFocus(focus);

	}

	/**
	 * 获取好友指定页的好友pagebean
	 */
	@Override
	public PageBean<User> getFriendsByPage(long id, int page) {
		PageBean<User> pb = new PageBean<User>();
		int pagesize = 5;
		int totalNum = focusDao.getFriendsNum(id, "2");
		List<User> msges = focusDao.getFriendsByPage(id, "2", (page - 1) * pagesize, pagesize);
		msges = CommonUtil.setInfo(msges);

		if (msges == null || msges.size() == 0) {
			return pb;
		}
		pb.setList(msges);
		pb.setNowNum(msges.size());
		pb.setPageSize(pagesize);
		pb.setTotalNum(totalNum);
		pb.setTotalPage(totalNum % pagesize == 0 ? totalNum / pagesize : totalNum / pagesize + 1);
		pb.setNowPage(page);
		return pb;
	}

	/**
	 * 获取指定页的关注我的用户的pagebean
	 */
	@Override
	public PageBean<User> getFocusMe(long id, int page) {
		PageBean<User> pb = new PageBean<User>();
		int pagesize = 5;
		int totalNum = focusDao.getFocusMeNum(id);
		List<User> users = focusDao.getFocusMeByPage(id, (page - 1) * pagesize, pagesize);
		users = CommonUtil.userSetInfo(id, users, focusDao);

		if (users == null || users.size() == 0) {
			return pb;
		}
		pb.setList(users);
		pb.setNowNum(users.size());
		pb.setPageSize(pagesize);
		pb.setTotalNum(totalNum);
		pb.setTotalPage(totalNum % pagesize == 0 ? totalNum / pagesize : totalNum / pagesize + 1);
		pb.setNowPage(page);
		return pb;
	}

	/**
	 * 获取我关注的的指定页的pagebean
	 */
	@Override
	public PageBean<Focus> findFocusByPage(long id, int page) {
		PageBean<Focus> pb = new PageBean<Focus>();
		int pagesize = 5;
		int totalNum = focusDao.getFocusNum(id);
		List<Focus> focuses = focusDao.getFocusByPage(id, (page - 1) * pagesize, pagesize);
		focuses = CommonUtil.focusSetInfo(id, focuses);
		if (focuses == null || focuses.size() == 0) {
			return pb;
		}
		pb.setList(focuses);
		pb.setNowNum(focuses.size());
		pb.setPageSize(pagesize);
		pb.setTotalNum(totalNum);
		pb.setTotalPage(totalNum % pagesize == 0 ? totalNum / pagesize : totalNum / pagesize + 1);
		pb.setNowPage(page);
		return pb;
	}

	/**
	 * 获取关注状态
	 * id:关注人；id2:被关注人
	 */
	@Override
	public String getFocusStatus(long id, long id2) {
		String status = "";
		Focus focus = focusDao.getCertainFocus(id, id2);
		if(focus==null) {
			status = "陌生人";
		}else if("1".equals(focus.getFocusStatus())) {
			status = "已关注";
		}else if("2".equals(focus.getFocusStatus())) {
			status = "好友";
		}
		return status;
	}

}
